//
//  WBMTOrderController.m
//  WBMTwaimai
//
//  Created by WangBiao on 16/9/21.
//  Copyright © 2016年 lsrain. All rights reserved.
// 

#import "WBMTOrderController.h"
#import "WBMTCategoryView.h"
#import "CZAdditions.h"
#import "MTBaseView.h"

//自定义宏
///顶部视图高度
#define KTopVHigh 123
///导航栏高度
#define KNavHight 64

@interface WBMTOrderController ()<UIScrollViewDelegate, UIGestureRecognizerDelegate>

///顶部视图
@property (nonatomic, weak) UIView *topView;
///分类视图
@property (nonatomic, weak) WBMTCategoryView *categoryV;
@end

@implementation WBMTOrderController

#pragma make - 搭建界面
- (void)setupUI{
    
    //顶部视图
    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_offset(KTopVHigh);
    }];
    self.topView = topView;
    
    //分类视图
    WBMTCategoryView *categeryView = [[WBMTCategoryView alloc]init];
    categeryView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:categeryView];
    [categeryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(topView.mas_bottom);
        make.height.mas_offset(37);
    }];
    
    //底部滚动内容视图
    UIScrollView *scroView = [self setupContentV];
    scroView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:scroView];
    [scroView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(categeryView.mas_bottom);
    }];
    
    //添加拖拽事件
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:pan];
    
    //设置代理
    scroView.delegate = self;
    pan.delegate = self;
    
    //赋值
    self.categoryV = categeryView;
    
    //定义block并赋值给分类视图等待回调
    categeryView.categoryBlock = ^(NSUInteger pageNum){
        
    [scroView scrollRectToVisible:CGRectMake(pageNum * scroView.bounds.size.width, 0, scroView.bounds.size.width, scroView.bounds.size.height) animated:YES];
        
    };
}

#warning MT-2 - 开启多手势支持/手势冲突
///要开启多手势支持，否则scroView和其他的控件的手势就不能兼容了（滚动scroView不能触发手势）
///2.还要设置手势的代理：pan.delegate = self;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

#pragma mark - 代理方法
///scroVeiw滚动时调用代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //防止从按钮1直接点击按钮3的时候，会在滚动的过程中将按钮2点亮一次
    // 如果不是人为的滚动scrollView,不执行设置偏移量操作!
    // 用户在拖拽 || 用户的手指在scrollView上 || 用户拖拽结束,松手后开始减速
    if (!(scrollView.isDragging || scrollView.isTracking || scrollView.isDecelerating)) {
        return;
    }
    
    //滚动时改变分类视图的黄线位置和按钮状态
    CGFloat off_X = scrollView.contentOffset.x;
    
    //categoryV需要的偏移量：off_X / 3
    self.categoryV.off_X = off_X / 3;
}


///底部内容视图
- (UIScrollView *)setupContentV{
    UIScrollView *scroV = [[UIScrollView alloc]init];
    
    scroV.pagingEnabled = YES;
    NSArray<NSString *> *arrVC = @[@"WBMTDishesController", @"WBMTAppriseController", @"WBMTMerChantController"];
    
    //临时数组
    NSMutableArray<UIView *> *arrVCM = [NSMutableArray arrayWithCapacity:arrVC.count];
    
    [arrVC enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        Class cls = NSClassFromString(obj);
        MTBaseViewController *vc = [[cls alloc] init];

#warning QU-1 - 建立父子控件关心
        //建立父子控件关系
        //响应者链条
        [self cz_addChildController:vc intoView:scroV];
        //将视图添加到数组中
        [arrVCM addObject:vc.view];
    }];
    
    //约束
    [arrVCM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [arrVCM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(scroV);
        make.top.bottom.equalTo(scroV);
    }];
    
    return scroV;
}

///拖拽事件实现方法
- (void)panAction:(UIPanGestureRecognizer *)sender{
    CGFloat offsetY = [sender translationInView:self.view].y;
    CGFloat height = self.topView.bounds.size.height + offsetY;
    
    //重置识别到的坐标（否则坐标原点不变，识别到的距离会依次递增）
    [sender setTranslation:CGPointZero inView:self.view];
    
    if (height > KTopVHigh) {
        height = KTopVHigh;
    } else if (height < KNavHight){
        height = KNavHight;
    }
    
    //更新约束
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(height);
    }];
    
//    [self.view layoutIfNeeded];
    
#warning CA-1 - 计算导航栏的透明度
    //1.单位透明度
    CGFloat percent = 1.0 / (KTopVHigh - KNavHight);
    //2.透明度
    CGFloat alpha = 1 - percent * (height - KNavHight);
    //更新导航栏透明度
    self.navigationController.navigationBar.alpha = alpha;
}

///生命周期
- (void)viewWillAppear:(BOOL)animated{
    
    //设置导航栏透明度为0
    self.navigationController.navigationBar.alpha = 0;
}


@end
